package com.quanyou.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.AbstractMessageSource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.util.Assert;

import java.util.Locale;

/**
 * Created by lishaoyan on 2015/5/20.
 * <p>
 * Error Code Rules
 * <p>
 * - an integer (>=0)
 * - range:
 * 0  succes, no error
 * 1 - 999: system errors or general errors
 * 1000 - 1999: user's errors
 * 2000 - 2999: bankcard's errors
 * 3000 - 3999: transaction's errors
 */
public class ErrorCode {
	private static final Logger logger = LoggerFactory.getLogger(ErrorCode.class);

	// error message prefix
	private static final String ERR_MESSAGE_FORMAT = "error.%d";

	// message bundle source
	private static AbstractMessageSource messageSource = null;

	// system error
	public static final int SYS_SUCCESS = 0;
	public static final int SYS_INVALID_FILE = 1;
	public static final int SYS_UP_REQ_ISNULL = 2;

	public static final int SYS_MOBILE_VERIFY_FAIL = 3;
	public static final int SYS_PARAMETER_ERROR = 4;
	public static final int SYS_SEND_VERIFY_CODE_ERROR = 5;
	public static final int SYS_JSON_FORMAT_ERROR = 6;
	public static final int SYS_TOKEN_INVALID = 7;
	public static final int SYS_VALIDATE_CODE_FAIL = 8;
	public static final int SYS_OP_TOO_FREQUENTLY = 9;
	public static final int SYS_FAIL = 10;
	public static final int SYS_DB_ERROR = 11;
	public static final int SYS_NOT_LOGIN_ERROR = 12;
	public static final int SYS_PAGING_PARAM_INVALID = 13; //分页参数无效

	public static final int SYS_BIZ_ERROR = 14;


	// business customer defined


	public static final int USER_NOT_FOUND = 1200;				// 用户未找到


	/**
	 * getSimpleRegionModel the error message of the specified error code
	 *
	 * @param errorCode the error code
	 * @param locale    the locale of error message
	 * @return the error messsage
	 */
	public static String getErrorMessage(int errorCode, Object[] objects, Locale locale, ReloadableResourceBundleMessageSource messageSource) {
		Assert.isTrue(errorCode >= 0, "error code must great than zero");

		if (messageSource == null) {
			logger.warn("can't getSimpleRegionModel message because message bundle source is null");
			return String.format("ErrorCode = %d. Message Source isn't ready.", errorCode);
		}

		Locale lc = (locale == null) ? Locale.CHINA : locale;
		String message = null;
		try {
			message = messageSource.getMessage(String.format(ERR_MESSAGE_FORMAT, errorCode), objects, lc);
		} catch (NoSuchMessageException e) {}

		if (message == null) {
			message = String.format("系统繁忙，请稍后重试（%d）", errorCode);
		}

		return message;
	}

	/**
	 * getSimpleRegionModel the error message of the specified error code
	 *
	 * @param errorCode the error code
	 * @return the error message with China Locale
	 */
	public static String getErrorMessage(int errorCode, ReloadableResourceBundleMessageSource messageSource) {
		return getErrorMessage(errorCode, null, null, messageSource);
	}

	/**
	 * decide if the message bundle source is set
	 *
	 * @return true if the message bunldes source is set, or false
	 */
	public static boolean isMessageSourceReady() {
		return messageSource != null;
	}

	/**
	 * set the message source bundle. it should be set before getMessage is invoked.
	 *
	 * @param messageSource the message bundle source
	 */
	public static void setMessageSource(AbstractMessageSource messageSource) {
		ErrorCode.messageSource = messageSource;
	}
}
